package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.AltHierarchyElementUtil;

/**
 * The complex type hierarchy is used by most or all the hierarchical elements of act-like documents.
 * 
 * <pre>
 * 	<xsd:complexType name="althierarchy">
 * 		<xsd:complexContent>
 * 			<xsd:extension base="basehierarchy">
 * 				<xsd:choice minOccurs="0" maxOccurs="unbounded">
 * 					<xsd:group ref="containerElements" />
 * 					<xsd:element ref="componentRef" />
 * 					<xsd:group ref="ANcontainers" />
 * 					<xsd:group ref="blockElements" />
 * 				</xsd:choice>
 * 				<xsd:attributeGroup ref="corereq" />
 * 			</xsd:extension>
 * 		</xsd:complexContent>
 *  </xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public abstract class AltHierarchy extends BaseHierarchyCoreReq {

	// Optional.
	private List<AltHierarchyElement> _elements;

	public List<AltHierarchyElement> getElements() {
		return _elements;
	}

	public void addElement(AltHierarchyElement element) {
		if (_elements == null) {
			_elements = new ArrayList<AltHierarchyElement>();
		}
		_elements.add(element);
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		super.writeExternal(out);
		if (_elements != null) {
			for (int i = 0, n = _elements.size(); i < n; i++) {
				_elements.get(i).writeExternal(out);
			}
		}
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		final String localName = in.getLocalName();
		super.readExternal(in);
		AltHierarchyElementUtil.read(in, _elements = new ArrayList<AltHierarchyElement>(4),localName);
	}

}